/**
 * @file	commander.h
 * @author	Mitch
 * @author 	David
 *
 * @brief	Header file for main commander.h
 *
 * @mainpage Project 2: Porting our Wireless Roomba Controller to RTOS.
 *
 * \section Introduction
 * This project requires porting the code of project 1 to RTOS.
 * In the first part of the assignment, we have splitted the code from project 1 into tasks.
 * We have separated the code into 4 tasks: Joystick, Radio Tx, Radio Rx, and uart. The joystick function is
 * responsible for checking whether the user has pressed a command, and to prepare a packet to be sent to the Roomba.
 * The radio Tx task is responsible for transmitting the packet containing a command to the Roomba. The radio Rx task
 * is responsible for receiving a status packet sent from the roomba and recording the distance and time (start and end)
 * to a data structure that will be later read by uart. Uart task is responsible for calculating the speed and printing
 * the result to the screen.
 *
 * \section structure Code Structure
 * This project contains three different main programs. Each main function correspond to a different scheduling policy.
 * The main programs are the following: commander_events.c, commander_periodic.c, and commander_system_round_robin.c.
 *
 * \section scenario1 Periodic Controller (commander_periodic.c)
 * The Periodic controller creates 4 periodic tasks. It will run joystick for 1 tick, radio tx for 2 ticks,
 * radio rx for 5 ticks, and uart for 4 ticks.
 *
 * \section scenario2 System and Round Robin Controller (commander_system_rr.c)
 * The System and Round Robin controller creates 2 round robin and 2 system tasks, joystick, uart and radio tx, radio rx,
 * respectively. System tasks are created and sleeps for some time until they wake up and check whether they can
 * execute.For instance, when a joystick command is received, it sets the joystick flag high and the radio tx task
 * will transmit the packet when it eventually wakes up.
 *
 * \section scenario3 Event Coordinator Controller (commander_events.c)
 * The Event coordinator controller also creates 2 round robin and 2 system tasks, joystick, uart and radio tx, radio rx,
 * respectively. In this scenario, the radio tx is blocked until the joystick receives a movement command and awakes the
 * radio tx task. Similarly, the radio rx system task is blocked until the ISR signals it when receiving a packet.
 * We have also used events for the uart task, which will block until the radio rx task receives a packet and is ready to awake
 * the uart task to print the speed.
 *
 */


#ifndef COMMANDER_H_
#define COMMANDER_H_

#include "uart.h"
#include "roomba_sci.h"
#include "radio.h"
#include "os.h"

#define clock8MHz()    CLKPR = _BV(CLKPCE); CLKPR = 0x00;	// Clock speed
#define STRING_SIZE		64	//can be changed to handle shorter/longer strings
#define TIME_DELAY		200	//200 ms
#define NUM_ARGS_BYTES	4	// The number of bytes taken up by arguments in the roomba commmand (packet.h)
//#define COMMANDER_PERIODIC
//#define COMMANDER_SYSTEM_RR
#define COMMANDER_EVENTS
//#define TEST_COMMANDER

/// The Roomba data structure for calculating speed.
typedef struct roomba_data {
	uint16_t start_time;
	uint16_t end_time;
	int16_t distance;
} ROOMBA_DATA;

void init_led();
void init_joystick();
void init_radio();
void task_uart(void);
void task_joystick(void);
void task_radio_send(void);
void task_radio_receive(void);

void set_roomba_command(radiopacket_t *packet, uint8_t command, uint8_t arguments[NUM_ARGS_BYTES]);

#endif
